import 'package:json_annotation/json_annotation.dart';

part 'store_total.g.dart';

@JsonSerializable()
class StoreTotal {
  final int storeId;
  final String storeName;
  final String storeLevelName;
  final String logoUrl;
  final String ownerName;
  final String businessName;
  final String businessLevel;
  final String businessDescr;
  final List<TotalItem> totalItems;
  final bool icpMode;
  final bool displayExchangeCoin;
  final bool displayTransferCoin;

  StoreTotal(
    this.storeId,
    this.storeName,
    this.storeLevelName,
    this.logoUrl,
    this.ownerName,
    this.businessName,
    this.businessLevel,
    this.businessDescr,
    this.totalItems,
    this.icpMode,
    this.displayExchangeCoin,
    this.displayTransferCoin,
  );

  factory StoreTotal.fromJson(Map<String, dynamic> json) => _$StoreTotalFromJson(json);

  Map<String, dynamic> toJson() => _$StoreTotalToJson(this);

  int total(String totalType) {
    return totalItems
        .firstWhere(
          (item) => item.totalType == totalType,
          orElse: () => null,
        )
        ?.total;
  }

  String totalInfo(String totalType) {
    return totalItems
        .firstWhere(
          (item) => item.totalType == totalType,
          orElse: () => null,
        )
        ?.totalInfo;
  }
}

@JsonSerializable()
class TotalItem {
  final String totalType;
  final String totalTypeName;
  final int total;
  final String totalInfo;

  TotalItem(
    this.totalType,
    this.totalTypeName,
    this.total,
    this.totalInfo,
  );

  factory TotalItem.fromJson(Map<String, dynamic> json) => _$TotalItemFromJson(json);

  Map<String, dynamic> toJson() => _$TotalItemToJson(this);
}
